package org.jeecg.modules.demo3.peijian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo3.peijian.entity.AxhkXxwyLinearRegulato;
import org.jeecg.modules.demo3.peijian.entity.materials.Rental;
import org.jeecg.modules.demo3.peijian.service.IAxhkXxwyLinearRegulatoService;
import org.jeecg.modules.demo3.peijian.utill.PdfUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 线性稳压
 * @Author: jeecg-boot
 * @Date:   2021-04-13
 * @Version: V1.0
 */
@Api(tags="线性稳压")
@RestController
@RequestMapping("/fly/axhkXxwyLinearRegulato")
@Slf4j
public class AxhkXxwyLinearRegulatoController extends JeecgController<AxhkXxwyLinearRegulato, IAxhkXxwyLinearRegulatoService> {
	@Autowired
	private IAxhkXxwyLinearRegulatoService axhkXxwyLinearRegulatoService;

	/**
	 * 分页列表查询
	 *
	 * @param axhkXxwyLinearRegulato
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "线性稳压-分页列表查询")
	@ApiOperation(value="线性稳压-分页列表查询", notes="线性稳压-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AxhkXxwyLinearRegulato axhkXxwyLinearRegulato,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AxhkXxwyLinearRegulato> queryWrapper = QueryGenerator.initQueryWrapper(axhkXxwyLinearRegulato, req.getParameterMap());
		Page<AxhkXxwyLinearRegulato> page = new Page<AxhkXxwyLinearRegulato>(pageNo, pageSize);
		IPage<AxhkXxwyLinearRegulato> pageList = axhkXxwyLinearRegulatoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param axhkXxwyLinearRegulato
	 * @return
	 */
	@AutoLog(value = "线性稳压-添加")
	@ApiOperation(value="线性稳压-添加", notes="线性稳压-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AxhkXxwyLinearRegulato axhkXxwyLinearRegulato) {
		axhkXxwyLinearRegulatoService.save(axhkXxwyLinearRegulato);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param axhkXxwyLinearRegulato
	 * @return
	 */
	@AutoLog(value = "线性稳压-编辑")
	@ApiOperation(value="线性稳压-编辑", notes="线性稳压-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AxhkXxwyLinearRegulato axhkXxwyLinearRegulato) {
		axhkXxwyLinearRegulatoService.updateById(axhkXxwyLinearRegulato);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "线性稳压-通过id删除")
	@ApiOperation(value="线性稳压-通过id删除", notes="线性稳压-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		axhkXxwyLinearRegulatoService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "线性稳压-批量删除")
	@ApiOperation(value="线性稳压-批量删除", notes="线性稳压-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.axhkXxwyLinearRegulatoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "线性稳压-通过id查询")
	@ApiOperation(value="线性稳压-通过id查询", notes="线性稳压-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AxhkXxwyLinearRegulato axhkXxwyLinearRegulato = axhkXxwyLinearRegulatoService.getById(id);
		if(axhkXxwyLinearRegulato==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(axhkXxwyLinearRegulato);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param axhkXxwyLinearRegulato
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AxhkXxwyLinearRegulato axhkXxwyLinearRegulato) {
        return super.exportXls(request, axhkXxwyLinearRegulato, AxhkXxwyLinearRegulato.class, "线性稳压");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AxhkXxwyLinearRegulato.class);
    }

	 //计算所有 现有数量 计算所有购买金额
	 @GetMapping("getByAxhkXxwyLinearRegulato")
	 public Result<?> getByAxhkXxwyLinearRegulato(){
		 Rental rental =  axhkXxwyLinearRegulatoService.AtAxhkXxwyLinearRegulato();
		 return Result.OK(rental);
	 }

	//点击移除 根据id 删除数据 添加数据
	@GetMapping("getByDSDate")
	public Result<?> getByDSDate(String id,int num){
		int i = axhkXxwyLinearRegulatoService.AtLinearRegulato(id,num);
		if(i>0){
			return Result.OK("成功！");
		}
		return Result.OK("不成功！");
	}

	//上传文件
	@GetMapping("getByUpdateXxwyLinearPath")
	public Result<?> getByUpdateXxwyLinearPath(String id,String names){
		int i = axhkXxwyLinearRegulatoService.AtUpdateAxhkXxwyLinearRegulato(id,names);
		return Result.OK(i);
	}

	//点击删除 通过id 修改path 并且删除本地文件
	@GetMapping("getByUpdatePath")
	public Result<?> getByUpdatePath(String id){
		int i= axhkXxwyLinearRegulatoService.AtuUpdateXxwyLinearRegulatoPath(id);
		return Result.OK(i);
	}

	@GetMapping("LookFile")
	public Result<?> getLookFile(String id){
		String filePath = axhkXxwyLinearRegulatoService.dataPath(id);
		if(null != filePath){
			return Result.OK(1);
		}else{
			return Result.OK(0);
		}
	}

	//浏览器查看上传pdf文件
	@GetMapping(value = "getByLookFile")
	public void getByLookFile(@RequestParam(value = "id",required = false) String id, HttpServletResponse response){
		InputStream inputStream = null;
		OutputStream outputStream = null;
		String filePath = axhkXxwyLinearRegulatoService.dataPath(id);
		if(filePath != null && "" != filePath){
			try {
				File file = new File(filePath);
				if (!file.exists()) {
					response.setStatus(404);
					throw new RuntimeException("文件不存在..");
				}
				response.setContentType("application/pdf");// 设置强制下载不打开
				response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
				inputStream = new BufferedInputStream(new FileInputStream(filePath));
				outputStream = response.getOutputStream();
				byte[] buf = new byte[inputStream.available()];
				int len;
				while ((len = inputStream.read(buf)) > 0) {
					outputStream.write(buf, 0, len);
				}
				response.flushBuffer();
			}catch (IOException e) {
				log.error("预览文件失败" + e.getMessage());
				response.setStatus(404);
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						log.error(e.getMessage(), e);
					}
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						log.error(e.getMessage(), e);
					}
				}
			}
		}
	}

	/*// pdf文件下载
	@GetMapping("downloadFile")
	public void downloadFile(@RequestParam(value = "id",required = false) String id, HttpServletResponse response){
		String fileName = axhkXxwyLinearRegulatoService.dataPath(id);
		if(null != fileName){
			PdfUtill.downFile(fileName,response);
		}
	}*/



}
